package com.midea.ai.air.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.midea.ai.air.R;
import com.midea.ai.air.common.Constant;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ouyangdi on 2016/3/10.
 * 首页电量显示柱状图
 */
public class ElectricChartView extends View {

    private int mWidth, mHeight; //View的宽高
    private Paint mPaintAbsText, mPaintOrdText, mPaintBlue, mPaintDash, mPaintCircle; //画笔

    private int mValueMax=8, mValueMin=2; //电量的最大值和最小值
    private List<String> mOrdinateVal = new ArrayList<>(); //纵坐标
    private List<String> mAbscissaVal = new ArrayList<>(); //横坐标

    private List<Path> mPathList = new ArrayList<>(); //虚线的path
    private List<Float> mCenterX = new ArrayList<>();  //圆的X坐标
    private float mCenterY; //圆的Y坐标
    private int mRadius; //圆的半径
    private List<Float> mBaseline = new ArrayList<>();  //纵坐标文本的baseline集合

    private float mOrTextMaxWidth; //纵坐标文本的最大宽度

    private float mAbsStartY; //横坐标文本的baseline

    private List<Float> mElecList = new ArrayList<>(); //用电量

    private float mPixPerDegree; //1度占多少像素

    public ElectricChartView(Context context) {
        this(context, null, 0);
    }

    public ElectricChartView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ElectricChartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mRadius = Constant.toPix(12)/2;

        initPaint();
        initData();
    }

    /**
     * 初始化X坐标和Y坐标
     */
    private void initData() {
        mElecList.add(3f);
        mElecList.add(5f);
        mElecList.add(6f);
        mElecList.add(2f);
        mElecList.add(1f);
        mElecList.add(9f);
        mElecList.add(1.5f);
        mElecList.add(1f);
        mElecList.add(9f);
        mElecList.add(1.5f);
        mElecList.add(3f);
        mElecList.add(5f);
        mElecList.add(6f);
        mElecList.add(2f);
        mElecList.add(1f);
        mElecList.add(9f);
        mElecList.add(3f);

        //遍历用电量链表,获取最大值和最小值
        mValueMax = mValueMin = Math.round(mElecList.get(0));
        for(int i=0; i<mElecList.size(); i++) {
            int val = Math.round(mElecList.get(i));
            if( val > mValueMax) {
                mValueMax = val;
            }
            if(val < mValueMin) {
                mValueMin = val;
            }
        }

        switch((mValueMax-mValueMin)%3) {
            case 1:
                mValueMin--;
                mValueMax++;
                break;
            case 2:
                mValueMin--;
                break;
        }

        int mDiv = (mValueMax-mValueMin)/3;
        mOrdinateVal.add(mValueMax + "");
        mOrdinateVal.add((mValueMax - mDiv) + "");
        mOrdinateVal.add((mValueMin + mDiv) + "");
        mOrdinateVal.add(mValueMin + "");

        //求取纵坐标文本的最大宽度
        mOrTextMaxWidth = mPaintOrdText.measureText(mOrdinateVal.get(0));
        for(int i=0; i<mOrdinateVal.size(); i++) {
            float width = mPaintOrdText.measureText(mOrdinateVal.get(i));
            if(width > mOrTextMaxWidth) {
                mOrTextMaxWidth = width;
            }
        }

        //初始化横坐标文本
        mAbscissaVal.add("1");
        for(int i=5; i<30; i+=5) {
            mAbscissaVal.add(i + "");
        }
        mAbscissaVal.add("31");

        mAbsStartY = Math.abs(mPaintAbsText.getFontMetrics().top);
    }

    /**
     * 初始化画笔
     */
    public void initPaint() {
        Resources resources = getResources();

        //横坐标文本画笔
        mPaintAbsText = new Paint();
        mPaintAbsText.setColor(resources.getColor(R.color.text_gray_assist));
        mPaintAbsText.setAntiAlias(true);
        mPaintAbsText.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, Constant.toPix(20), resources.getDisplayMetrics()));
        mPaintAbsText.setTextAlign(Paint.Align.CENTER);

        //纵坐标文本画笔
        mPaintOrdText = new Paint();
        mPaintOrdText.setColor(resources.getColor(R.color.text_gray_assist));
        mPaintOrdText.setAntiAlias(true);
        mPaintOrdText.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, Constant.toPix(20), resources.getDisplayMetrics()));
        mPaintOrdText.setTextAlign(Paint.Align.RIGHT);

        //圆圈画笔
        mPaintCircle = new Paint();
        mPaintCircle.setColor(resources.getColor(R.color.text_gray_assist));
        mPaintCircle.setAntiAlias(true);

        //柱状画笔
        mPaintBlue = new Paint();
        //颜色待定
        mPaintBlue.setColor(resources.getColor(R.color.electric_histogram));
        mPaintBlue.setAntiAlias(true);
        mPaintBlue.setStyle(Paint.Style.FILL);

        mPaintDash = new Paint();
        mPaintDash.setColor(resources.getColor(R.color.text_gray_assist));
        mPaintDash.setAntiAlias(true);
        mPaintDash.setStyle(Paint.Style.STROKE);
        PathEffect effects = new DashPathEffect(new float[]{Constant.toPix(4),Constant.toPix(8)},1);
        mPaintDash.setPathEffect(effects);
        mPaintDash.setStrokeWidth(Constant.toPix(2));

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;

        //初始化横坐标虚线Path
        Path path = new Path();
        int lineY;
        float top = Math.abs(mPaintOrdText.getFontMetrics().top);
        float bottom = Math.abs(mPaintOrdText.getFontMetrics().bottom);
        float space = top - (top+bottom)/2f;
        float baseLine;
        int dis = Constant.toPix(45);
        for(int i=1; i<5; i++) {
            lineY = i*dis;
            path.moveTo(Constant.toPix(30), lineY);
            path.lineTo(w, lineY);
            mPathList.add(path);
            baseLine = lineY + space;
            mBaseline.add(baseLine);
        }

        mPixPerDegree = (float)(3*dis)/(float)(mValueMax - mValueMin);

        //初始化圆圈的横坐标列表及纵坐标
        float div = (w-Constant.toPix(30)-mRadius*2*31-2*Constant.toPix(6))/30f + mRadius*2;
        for(int i=0; i<30; i++) {
            mCenterX.add(Constant.toPix(30) + Constant.toPix(6) + mRadius + i*div);
        }
        mCenterX.add((float) (w - mRadius - Constant.toPix(6)));
        mCenterY = h-mRadius;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawAbscissaLine(canvas);
        drawCircle(canvas);
        drawAbscissaText(canvas);
        drawOrdinateText(canvas);
        drawHistogram(canvas);
    }

    /**
     * 绘制横坐标线(虚线)
     */
    private void drawAbscissaLine(Canvas canvas) {
        for(int i=0; i<mPathList.size(); i++) {
            canvas.drawPath(mPathList.get(i), mPaintDash);
        }
    }

    /**
     * 绘制纵坐标文本
     */
    private void drawOrdinateText(Canvas canvas) {
        for(int i=0; i<mOrdinateVal.size(); i++) {
            canvas.drawText(mOrdinateVal.get(i), mOrTextMaxWidth, mBaseline.get(i), mPaintOrdText);
        }
    }

    /**
     * 绘制横坐标文本
     */
    private void drawAbscissaText(Canvas canvas) {
        canvas.drawText(mAbscissaVal.get(0), mCenterX.get(0), mAbsStartY, mPaintAbsText);

        canvas.drawText(mAbscissaVal.get(mAbscissaVal.size()-1), mCenterX.get(mCenterX.size()-1), mAbsStartY, mPaintAbsText);

        for(int i=5; i<30; i+=5) {
            canvas.drawText(mAbscissaVal.get(i/5), mCenterX.get(i-1), mAbsStartY, mPaintAbsText);
        }
    }

    /**
     * 绘制圆圈
     */
    private void drawCircle(Canvas canvas) {
        for(int i=mElecList.size(); i<mCenterX.size(); i++) {
            canvas.drawCircle(mCenterX.get(i), mCenterY, mRadius, mPaintCircle);
        }
    }

    /**
     * 绘制柱状图
     */
    private void drawHistogram(Canvas canvas) {
        RectF rect = new RectF();
        float axisX;

        for(int i=0; i<mElecList.size(); i++) {
            axisX = mCenterX.get(i);

            rect.left = axisX-mRadius;
            rect.right = axisX+mRadius;
            rect.top = (mValueMax-mElecList.get(i))*mPixPerDegree + Constant.toPix(45);
            rect.bottom = mCenterY+mRadius;

            canvas.drawRoundRect(rect, mRadius, mRadius, mPaintBlue);
        }
    }
}